#include "listmodel.h"

ListModel::ListModel(QObject *parent)
    : QAbstractListModel(parent)
{
    QHash<int, QByteArray> roles;

    roles[RoleItemName] = "name";
    roles[RoleListItemId] = "listItemId";
    roles[RoleQuantity] = "quantity";
    roles[RoleBought] = "bought";
    roles[RoleUnit] = "unit";
    roles[RoleItemId] = "itemId";
    roles[RoleGroupId] = "groupId";
    roles[RoleGroupName] = "groupName";
    roles[RoleIsAGroup] = "isAGroup";
    roles[RoleGroupNumber] = "groupNumber";
    roles[RoleIndex] = "index";

    setRoleNames(roles);
}

void ListModel::addListItem(const ElementPopisa &elementPopisa, bool isAGroup, int groupNumber, int index){
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_listItems<<new ListItem(elementPopisa.dajId(),elementPopisa.dajArtikl(),elementPopisa.dajKolicinu(),
                              elementPopisa.jeKupljen(),elementPopisa.dajMjernuJednincu(), isAGroup, groupNumber, index);

    connect(m_listItems.last(), SIGNAL(dataChanged()), SLOT(handleItemChange()));

    qDebug()<<"dodajem element"<<elementPopisa.dajArtikl().dajImeArtikla();
    endInsertRows();
    emit countChanged(m_listItems.count());
}

int ListModel::rowCount(const QModelIndex & parent) const {
    return m_listItems.count();
}

int ListModel::count(){
    return m_listItems.count();
}

void ListModel::clear(){
    int currentCount=m_listItems.count();
    for (int i=0; i<currentCount; i++)
    {
        beginRemoveRows(QModelIndex(), 0, 0);

        ListItem* item=m_listItems.takeFirst();
        delete item;

        emit countChanged(m_listItems.count());
        endRemoveRows();
    }
}

ListItem* ListModel::get(int index){
    return m_listItems[index];
}


Qt::ItemFlags ListModel::flags(const QModelIndex &index) const{
    if (!index.isValid())
        return 0;

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant ListModel::headerData(int section, Qt::Orientation orientation,
                               int role) const
{
    return QVariant();
}

QVariant ListModel::data(const QModelIndex & index, int role) const  {
    if (index.row() < 0 || index.row() > m_listItems.count())
        return QVariant();

    ListItem* listItem=m_listItems[index.row()];

    if (role == RoleItemName)
        return listItem->itemName();
    else if (role == RoleListItemId)
        return listItem->listItemId();
    else if (role == RoleQuantity)
        return listItem->quantity();
    else if (role == RoleBought)
        return listItem->isBought();
    else if (role == RoleUnit)
        return listItem->unit();
    else if (role == RoleItemId)
        return listItem->itemId();
    else if (role == RoleGroupId)
        return listItem->groupId();
    else if (role == RoleGroupName)
        return listItem->groupName();
    else if (role == RoleIsAGroup)
        return listItem->isAGroup();
    else if (role == RoleGroupNumber)
        return listItem->groupNumber();
    else if (role == RoleIndex)
        return listItem->index();

    return QVariant();
}

QModelIndex ListModel::indexFromItem(const ListItem *item) const
{
    Q_ASSERT(item);
    for(int row=0; row<m_listItems.size(); ++row) {
        if(m_listItems.at(row) == item) return index(row);
    }
    return QModelIndex();
}

void ListModel::handleItemChange()
{
    ListItem* item = static_cast<ListItem*>(sender());
    QModelIndex index = indexFromItem(item);
    if(index.isValid())
        emit dataChanged(index, index);
}
